package zhouzhouyongche.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import zhouzhouyongche.model.Order;

@Repository
public class OrderDao extends HibernateDao<Order> {

	public void cancelOrder(String orderCode) {
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction tx = null;
		try {
			sessionFactory = getSessionFactory();
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			
			String hql = "update Order o set o.status = ? where o.orderCode = ?";
			Query q = session.createQuery(hql);
			q.setInteger(0, 3);
			q.setString(1, orderCode);
			q.executeUpdate();
			
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) 
				tx.rollback();
		} finally {
			session.close();
		}
	}

	public List<Order> getOrders(int userId) {
		String hql = "select u.orders "
				+ "from User as u "
				+ "where u.id = ?";
		Query q = getCurrentSession().createQuery(hql);
		q.setInteger(0, userId);
		return q.list();
	}
	
}
